<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="abnormalList" @selection-change="handleSelectionChange">
      <el-table-column type="expand" align="left">
        <template #default="scope">
          <el-table :data="scope.row.jcAbnormalDetailList">
            <el-table-column label="字段名称" align="center" prop="source" />
            <el-table-column label="原始数据" align="center" prop="sourceValue" />
            <el-table-column label="新数据" align="center" prop="targerValue" />
            <el-table-column label="备注" align="center" prop="description" />
          </el-table>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="left"/>
      <el-table-column label="所属组织机构" align="left" prop="jcOrganizationName"/>
      <el-table-column label="异常单日期" align="left" prop="time"/>
      <el-table-column label="异常原因" align="center" prop="description" width="250"/>
      <el-table-column label="修改数量" align="center" prop="number"/>
      <el-table-column label="运单号" align="center" prop="jcShipment.code"/>
      <el-table-column width="100" label="运单日期" align="center" prop="jcShipment.time">
        <template #default="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="运单状态" align="center">
        <template #default="scope">
          <dict-tag :options="dict.type.OrderStatus" :value="scope.row.jcShipment.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createName"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listShipmentAbnormal, getAbnormal} from "@/api/extend/abnormal";

export default {
  name: "ShipmentAbnormal",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedJcAbnormalDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 异常表格数据
      abnormalList: [],
      // 异常明细表格数据
      jcAbnormalDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // $comment时间范围
      daterangeCreateTime: [],
      // $comment时间范围
      daterangeModifyTime: [],
      // $comment时间范围
      daterangeTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createName: null,
        description: null,
        modifyName: null,
        modifyTime: null,
        name: null,
        status: null,
        number: null,
        time: null,
        type: null,
        jcOrderId: null,
        jcOrganizationId: null,
        jcShipmentId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询异常列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeModifyTime && '' != this.daterangeModifyTime) {
        this.queryParams.params["beginModifyTime"] = this.daterangeModifyTime[0];
        this.queryParams.params["endModifyTime"] = this.daterangeModifyTime[1];
      }
      if (null != this.daterangeTime && '' != this.daterangeTime) {
        this.queryParams.params["beginTime"] = this.daterangeTime[0];
        this.queryParams.params["endTime"] = this.daterangeTime[1];
      }
      listShipmentAbnormal(this.queryParams).then(response => {
        this.abnormalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createName: null,
        createTime: null,
        description: null,
        modifyName: null,
        modifyTime: null,
        name: null,
        status: null,
        number: null,
        time: null,
        type: null,
        jcOrderId: null,
        jcOrganizationId: null,
        jcShipmentId: null
      };
      this.jcAbnormalDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeModifyTime = [];
      this.daterangeTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加异常";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAbnormal(id).then(response => {
        this.form = response.data;
        this.jcAbnormalDetailList = response.data.jcAbnormalDetailList;
        this.open = true;
        this.title = "修改异常";
      });
    },
    /** 异常明细序号 */
    rowJcAbnormalDetailIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 异常明细添加按钮操作 */
    handleAddJcAbnormalDetail() {
      let obj = {};
      obj.createName = "";
      obj.description = "";
      obj.modifyName = "";
      obj.modifyTime = "";
      obj.name = "";
      obj.status = "";
      obj.source = "";
      obj.sourceValue = "";
      obj.targer = "";
      obj.targerValue = "";
      this.jcAbnormalDetailList.push(obj);
    },
    /** 异常明细删除按钮操作 */
    handleDeleteJcAbnormalDetail() {
      if (this.checkedJcAbnormalDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的异常明细数据");
      } else {
        const jcAbnormalDetailList = this.jcAbnormalDetailList;
        const checkedJcAbnormalDetail = this.checkedJcAbnormalDetail;
        this.jcAbnormalDetailList = jcAbnormalDetailList.filter(function (item) {
          return checkedJcAbnormalDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleJcAbnormalDetailSelectionChange(selection) {
      this.checkedJcAbnormalDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('extend/abnormal/export', {
        ...this.queryParams
      }, `abnormal_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
